//
//  LWMainTabBar.swift
//  ios8-01 weibo框架搭建
//
//  Created by blue on 16/2/27.
//  Copyright © 2016年 honeywell. All rights reserved.
//

import UIKit

//定义代理
@objc protocol  LWMainTabBarDelegate: NSObjectProtocol {
    
    //定义按钮被点击的调用的方法
    
    //optional表示代理方法是可选的
    optional
    func  mainTabBarComposeClick()
    
    
}

class LWMainTabBar: UITabBar {
    
    //定义代理属性
    weak  var composeDelegate :LWMainTabBarDelegate?
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        
        //布局子视图
        print("compose:\(composeBtn)")
        
        
        
        let  wide = self.frame.width / 5
        let  height = self.frame.height
    
        
        composeBtn.frame = CGRect(x: 0, y: 0, width: wide, height: height)
        
        //设置加号按钮的中心点
        
        //composeBtn.center = self.center  为什么不行？？？
        composeBtn.center = CGPoint(x: self.frame.width*0.5, y: self.frame.height*0.5)

    }
    
    
    
    
    
    
//懒加载
    lazy  var  composeBtn: UIButton = {
        
        let  btn = UIButton()
        //设置加号按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
        //将加号按钮添加到父视图
        self.addSubview(btn)
        
        //自动适应大小
        btn.sizeToFit()
        
        //给按钮添加点击事件
        btn.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    
    //点击事件
    func  composeBtnClick(){
        
        print("点击了加号")
        
        //3调用代理方法
        self.composeDelegate?.mainTabBarComposeClick?()
        
    }

    

}
